package com.glory.wxmemsicpda.activity

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.adapter.ChangeMaterialsListAdapter
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.MaterialsListBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.*
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils
import kotlinx.android.synthetic.main.activity_materials_list.*
import kotlinx.android.synthetic.main.activity_work_begin.*
import kotlinx.android.synthetic.main.activity_work_begin.llMain
import java.util.*

class MaterialsListActivity : BaseDialogActivity(), ChangeMaterialsListAdapter.CallBack {
    private var eqpId: String = ""
    private var mDataList: MutableList<MaterialsListBean.MaterialsBean> = mutableListOf()
    private var changeMaterialsListAdapter: ChangeMaterialsListAdapter = ChangeMaterialsListAdapter(
        mDataList
    )

    override fun getLayoutResId(): Int {
        return R.layout.activity_materials_list
    }

    override fun initView() {
        setTitle("材料列表")
    }

    override fun initData() {
        eqpId = intent.getStringExtra("showPlugin")!!
        tvEqpID.text = eqpId

        rvMaterialsList.layoutManager = LinearLayoutManager(this)
        rvMaterialsList.adapter = changeMaterialsListAdapter
        changeMaterialsListAdapter.setCallBack(this)

        requestData()
    }

    private fun requestData() {
//        var ss = "{\"Response\":{\"Header\":{\"TRANSACTIONID\":\"c5e5e318389d4c39b914e4c10ba519d4\",\"RESULT\":\"SUCCESS\",\"RESULTCODE\":null,\"RESULTMESSAGE\":null},\"Body\":{\"EQP_ID\":\"MO-001\",\"MTM_MAT_LIST\":[{\"MATERIAL_NO\":\"WL0001\",\"MATERIAL_ID\":\"T2-000011\",\"MATERIAL_TYPE\":\"4KUAISU1\",\"MATERIAL_NAME\":\"晶粒\",\"ONEQP_TIME\":\"2021-01-28T17:08:53\"},{\"MATERIAL_NO\":\"WL0002\",\"MATERIAL_ID\":\"T2-000012\",\"MATERIAL_TYPE\":\"4KUAISU2\",\"MATERIAL_NAME\":\"焊条\",\"ONEQP_TIME\":\"2021-01-28T17:08:53\"}]}}}"
//        var dd : BaseResponseBean<MaterialsListBean> = Gson().fromJson(ss,object : TypeToken<BaseResponseBean<MaterialsListBean>>(){}.type)
//        if (dd!!.isSuccess(true, supportFragmentManager)) {
//            mDataList.clear()
//            if (dd!!.response.body!!.mtM_MAT_LIST.size > 0) {
//                mDataList.addAll(dd!!.response.body!!.mtM_MAT_LIST)
//            }
//            changeMaterialsListAdapter.notifyDataSetChanged()
//        }
        val map = JsonObject()
        map.addProperty("EQP_ID", eqpId)
        NetUtils.requestPosNet(this,
            "PDAEQPMatList", "PDAEQPMatList",
            map,
            object :
                FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<MaterialsListBean?>?>(this) {
                override fun onSuccess(response: Response<BaseResponseBean<MaterialsListBean?>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(true, supportFragmentManager)) {
                        mDataList.clear()
                        if (response.body()!!.response.body!!.mtM_MAT_LIST.size > 0) {
                            mDataList.addAll(response.body()!!.response.body!!.mtM_MAT_LIST)
                        }
                        changeMaterialsListAdapter.notifyDataSetChanged()
                    }
                }

                override fun onError(response: Response<BaseResponseBean<MaterialsListBean?>?>) {
                    super.onError(response)
                    loadError(response.exception, "PDAEQPMatList")
                }
            })
    }

    override fun onRestart() {
        super.onRestart()
        requestData()
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity("header", llMain)
    }

    override fun down(materialsBean: MaterialsListBean.MaterialsBean) {
        requestDown(materialsBean)
    }

    private fun requestDown(materialsBean: MaterialsListBean.MaterialsBean) {
        val map = JsonObject()
        map.addProperty("EQP_ID", eqpId)
        map.addProperty("MATERIAL_TYPE", materialsBean.materiaL_TYPE)
        map.addProperty("MATERIAL_NO", materialsBean.materiaL_NO)
        NetUtils.requestPosNet(this,
            "PDAEQPMatUnload", "PDAEQPMatUnload",
            map,
            object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<*>?>(this) {
                override fun onSuccess(response: Response<BaseResponseBean<*>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(false, supportFragmentManager)) {
                        showToast("下机成功", true)
                        finish()
                    }
                }

                override fun onError(response: Response<BaseResponseBean<*>?>) {
                    super.onError(response)
                    loadError(response.exception, "PDAEQPMatUnload")
                }
            })
    }

    override fun change(materialsBean: MaterialsListBean.MaterialsBean) {
        val bundle = Bundle()
        bundle.putSerializable("MATERIAL_BEAN", materialsBean)
        bundle.putString("EQP_ID", eqpId)
        bundle.putString("APPEND", "NO")
        FreeApi_Utils.skipWithExtraIntent(
            "header",
            bundle,
            this@MaterialsListActivity,
            ChangeMaterialsActivity::class.java
        )
    }

    override fun append(materialsBean: MaterialsListBean.MaterialsBean) {
        val bundle = Bundle()
        bundle.putSerializable("MATERIAL_BEAN", materialsBean)
        bundle.putString("EQP_ID", eqpId)
        bundle.putString("APPEND", "YES")
        FreeApi_Utils.skipWithExtraIntent(
            "header",
            bundle,
            this@MaterialsListActivity,
            AppendMaterialActivity::class.java
        )
    }
}